package com.ruoyi.project.erp.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.erp.dto.ProjectUserQuery;
import com.ruoyi.project.erp.dto.TopProjectDTO;
import com.ruoyi.project.erp.vo.ProjectUserListVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.erp.domain.ProjectUser;
import com.ruoyi.project.erp.service.IProjectUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 项目成员Controller
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@RestController
@RequestMapping("/erp/projectUser")
public class ProjectUserController extends BaseController
{
    @Autowired
    private IProjectUserService projectUserService;

    /**
     * 查询项目成员列表
     */
    @PreAuthorize("@ss.hasPermi('erp:projectUser:list')")
    @GetMapping("/list")
    // 2. 将方法参数从 ProjectUser projectUser 更改为 ProjectUserQuery query
    public TableDataInfo list(ProjectUserQuery query)
    {
        startPage();
        List<ProjectUserListVO> list = projectUserService.selectProjectUserVoList(query);
        return getDataTable(list);
    }

    /**
     * 导出项目成员列表
     */
    @PreAuthorize("@ss.hasPermi('erp:projectUser:export')")
    @Log(title = "项目成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectUser projectUser)
    {
        List<ProjectUser> list = projectUserService.selectProjectUserList(projectUser);
        ExcelUtil<ProjectUser> util = new ExcelUtil<ProjectUser>(ProjectUser.class);
        util.exportExcel(response, list, "项目成员数据");
    }

    /**
     * 获取项目成员详细信息
     */
//    @PreAuthorize("@ss.hasPermi('erp:projectUser:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(projectUserService.selectProjectUserById(id));
//    }
    /**
     * 【新增接口】获取用于前端回显的项目成员详细视图信息
     * @param id 项目成员主键ID
     * @return AjaxResult 包含 ProjectUserListVO 对象
     */
    @PreAuthorize("@ss.hasPermi('erp:projectUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getProjectUserVoInfo(@PathVariable("id") Long id)
    {
        ProjectUserListVO projectUserVo = projectUserService.selectProjectUserVoById(id);
        return success(projectUserVo);
    }
    /**
     * 新增项目成员
     */
    @PreAuthorize("@ss.hasPermi('erp:projectUser:add')")
    @Log(title = "项目成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectUser projectUser)
    {
        return toAjax(projectUserService.insertProjectUser(projectUser));
    }

    /**
     * 修改项目成员
     */
    @PreAuthorize("@ss.hasPermi('erp:projectUser:edit')")
    @Log(title = "项目成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectUser projectUser)
    {
        return toAjax(projectUserService.updateProjectUser(projectUser));
    }

    /**
     * 删除项目成员
     */
    @PreAuthorize("@ss.hasPermi('erp:projectUser:remove')")
    @Log(title = "项目成员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectUserService.deleteProjectUserByIds(ids));
    }



    /*
     * 置顶项目
     */
    @Log(title = "项目成员", businessType = BusinessType.UPDATE)
    @PostMapping("/top")
    public AjaxResult top(@RequestBody TopProjectDTO topProjectDTO) {
        // 从Security上下文中获取当前登录用户的钉钉ID (dd_user_id)
        // 确保你的LoginUser中的SysUser对象包含了ddUserId这个属性
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUser() == null || loginUser.getUser().getDdUserId() == null) {
            return AjaxResult.error("获取当前用户信息失败，无法进行置顶操作");
        }
        String ddUserId = loginUser.getUser().getDdUserId();

        // 调用Service层执行更新操作
        projectUserService.topProject(topProjectDTO.getProjectId(), ddUserId, topProjectDTO.getTop());
        return AjaxResult.success("操作成功");
    }
}
